package com.cjm.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

public class LengthOfLongestSubstring3 {
    public static int lengthOfLongestSubstring(String s) {
        int lenMax = 0;
        int left = 0;
        int right = -1;

        int[] freq = new int[256];
        Map<String, Integer> map = new HashMap<>();
        while (left < s.length()) {
            if (right + 1 < s.length() && map.get(String.valueOf(s.charAt(right + 1))) == null) {
                right++;
                map.put(String.valueOf(s.charAt(right)), 1);
            } else {
                map.put(String.valueOf(s.charAt(left)), null);
                left++;
            }
            /*if (right + 1 < s.length() && freq[s.charAt(right + 1)] == 0) {
                right++;
                freq[s.charAt(right)]++;
            } else {
                freq[s.charAt(left++)]--;
            }
            */

            lenMax = Math.max(lenMax, right - left + 1);
        }

        return lenMax;
    }

    public static void main(String[] args) {
//        System.err.println(lengthOfLongestSubstring("abcabcbb"));
        // abcdca
//        System.err.println(lengthOfLongestSubstring("bbbbb"));
//        System.err.println(lengthOfLongestSubstring("pwwkew"));
        System.err.println(lengthOfLongestSubstring("abcdcafh"));
    }
}
